package com.dayuanit.dy17.library.controller;

import com.dayuanit.dy17.library.dto.ResponseDTO;
import com.dayuanit.dy17.library.entity.Book;
import com.dayuanit.dy17.library.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
public class BookController extends BaseController {

    @Autowired
    private IBookService bookService;

    public BookController() {
        System.out.println("---------BookController()----------");
    }


    @RequestMapping("/book/queryBook.do")
    public ResponseDTO queryBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bookName = req.getParameter("bookName");
        String authorName = req.getParameter("authorName");
        String pressName = req.getParameter("pressName");

        try {
            List<Book> books = bookService.queryBook(bookName, pressName, authorName);
            return ResponseDTO.success(books);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDTO.fail(e.getMessage());
        }
    }
}
